<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="工艺名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="technologyName">
              <a-input v-model="model.technologyName" placeholder="请输入工艺名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工厂" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="factoryId">
              <j-dict-select-tag type="list" v-model="model.factoryId"
                                 dictCode="mes_base_factory,factory_name,id,factory_level=1 and factory_status=1"
                                 placeholder="请选择工厂"
                                 :useCache="false"
                                 @change="choseFactory"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="车间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workshopId">
              <!--mes_base_factory,factory_name,id,factory_level=2 and factory_status=1-->
              <j-dict-select-tag type="list" v-model="model.workshopId" :dictCode="workshopDicCode"
                                 placeholder="请选择车间"
                                 :useCache="false"
                                 :disabled="workshopStatus"
                                 @change="choseWorkshop"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产线" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lineId">
              <!--mes_base_factory,factory_name,id,factory_level=3 and factory_status=1-->
              <j-dict-select-tag type="list" v-model="model.lineId" :dictCode="lineDicCode"
                                 placeholder="请选择产线"
                                 :useCache="false"
                                 :disabled="lineStatus"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
          </a-col>
          <a-col :span="12">
            <div class="box" v-if="confirmLoading">
              <a-button type="primary" @click="submitForm">确定</a-button>&nbsp;&nbsp;&nbsp;
              <a-button @click="close">取消</a-button>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MesTechnologyForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        workshopStatus: false,
        workshopDicCode: 'mes_base_factory,factory_name,id,factory_level=2 and factory_status=1',
        lineStatus: false,
        lineDicCode: 'mes_base_factory,factory_name,id,factory_level=3 and factory_status=1',
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          technologyName: [
            { required: true, message: '请输入工艺名称!' }
          ],
          factoryId: [
            { required: true, message: '请输入工厂!' }
          ],
          workshopId: [
            { required: true, message: '请输入车间!' }
          ],
          lineId: [
            { required: true, message: '请输入产线!' }
          ]
        },
        url: {
          add: '/mes/mesTechnology/add',
          edit: '/mes/mesTechnology/edit',
          queryById: '/mes/mesTechnology/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))
      if (!this.disabled) {
        this.workshopStatus = true
        this.lineStatus = true
      }
    },
    methods: {
      //自己添加的方法--------start
      choseFactory() {
        if (this.model.factoryId) {
          //设置车间能选择
          this.workshopStatus = false
          this.workshopDicCode = 'mes_base_factory,factory_name,id,factory_level=2 and factory_status=1 and pid=\'' + this.model.factoryId + '\''
        } else {
          //清空车间和产线
          this.model.workshopId = ''
          this.model.lineId = ''
          //设置车间和产线不能用
          this.workshopStatus = true
          this.lineStatus = true
        }
      },
      choseWorkshop() {
        if (this.model.workshopId) {
          //设置产线能选择
          this.lineStatus = false
          this.lineDicCode = 'mes_base_factory,factory_name,id,factory_level=3 and factory_status=1 and pid=\'' + this.model.workshopId + '\''
        } else {
          //清空产线
          this.model.lineId = ''
          //设置产线不能用
          this.lineStatus = true
        }
      },
      close(){
        this.$emit('close')
      },
      //自己添加的方法----------end

      add() {
        this.edit(this.modelDefault)
      },
      edit(record) {
        if (record && JSON.stringify(record) !== '{}') {
          //如果record存在，证明是编辑页面
          this.workshopStatus = false
          this.workshopDicCode = 'mes_base_factory,factory_name,id,factory_level=2 and factory_status=1 and pid=\'' + this.model.factoryId + '\''
          this.lineStatus = false
          this.lineDicCode = 'mes_base_factory,factory_name,id,factory_level=3 and factory_status=1 and pid=\'' + this.model.workshopId + '\''
        }
        this.model = Object.assign({}, record)
        this.visible = true
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      }
    }
  }
</script>
<style scoped>
  .box {
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
</style>